package com.DataFactory.user.utils;

public class Utils {
    public static byte[] hexStringToBytes(String hexString) {
        int len = hexString.length();
        if (len % 2 != 0) {
            throw new IllegalArgumentException("输入的十六进制字符串的长度必须是偶数");
        }
        byte[] result = new byte[len / 2];
        char[] hexChars = hexString.toCharArray();
        for (int i = 0; i < len; i += 2) {
            int pos = i / 2;
            result[pos] = (byte) (charToByte(hexChars[i]) << 4 | charToByte(hexChars[i + 1]));
        }
        return result;
    }
    private static int charToByte(char c) {
        if (c >= '0' && c <= '9') {
            return c - '0';
        }
        if (c >= 'a' && c <= 'f') {
            return c - 'a' + 10;
        }
        if (c >= 'A' && c <= 'F') {
            return c - 'A' + 10;
        }
        throw new IllegalArgumentException("无效的十六进制字符: " + c);
    }
}
